#include <bits/stdc++.h>
using namespace std;


int main() {
    ios_base::sync_with_stdio(false);cin.tie(nullptr);
    int T; cin >> T;
    while(T--){
        int N, tv, tp, tl, tu, te, limit;
        cin >> N >> tv >> tp >> tl >> tu >> te >> limit;
        vector<queue<string> > pro (N+1);
        deque<int> wait;
        queue<int> block;
        vector<int> v(26, 0);
        for (int i = 1; i <= N; ++i) {
            string in;
            wait.push_back(i);
            do{
                getline(cin, in);
                while(in.empty()) getline(cin, in);
                pro[i].push(in);
            } while(in != "end");
        }

        bool lock = false;
        while(!wait.empty() || !block.empty()){
            int id = wait.front(); wait.pop_front();
            int t = limit;
            bool jump = false;
            while(t > 0){
                string cmd = pro[id].front();
                stringstream ss(cmd);
                string first; ss >> first;
                if(first != "lock") pro[id].pop();
                if(first == "print"){
                    char val; ss >> val;
                    cout << id << ": " << v[val - 'a'] << "\n";
                    t -= tp;
                }
                else if(first == "lock"){
                    if(!lock) {
                        lock = true;
                        pro[id].pop();
                    }
                    else{
                        block.push(id);
                        jump = true;
                        break;
                    }
                    t -= tl;
                }
                else if(first == "unlock"){
                    lock = false;
                    if(!block.empty()){
                        int tmp = block.front(); block.pop();
                        wait.push_front(tmp);
                    }
                    t -= tu;
                }
                else if(first == "end"){
                    t -= te;
                    break;
                }
                else {
                    int tmp; char ch;
                    ss >> ch; ss >> tmp;
                    v[first[0] - 'a'] = tmp;
                    t -= tv;
                }
            }
            if (!pro[id].empty() && !jump) wait.push_back(id);
        }
        if(T) cout << "\n";
    }
    return 0;
}